package ar.edu.unq.mongodbexample.persistence;

import java.net.UnknownHostException;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * TODO: description
 */
public class MongoDBManager {
    private static MongoDBManager instance = null;

    private Mongo mongo;

    private Morphia morphia = new Morphia();

    private Datastore dataStore;

    private MongoDBManager() {

        try {
            this.setMongo(new Mongo("127.0.0.1", 27017));
            this.setDataStore(this.getMorphia().createDatastore(this.getMongo(), "universities"));
            this.getDataStore().ensureCaps();

        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (MongoException e) {
            throw new RuntimeException(e);
        }

    }

    public static MongoDBManager getInstance() {
        if (instance == null) {
            instance = new MongoDBManager();
        }
        return instance;
    }

    public Mongo getMongo() {
        return this.mongo;
    }

    public void setMongo(final Mongo mongo) {
        this.mongo = mongo;
    }

    public void setDataStore(final Datastore dataStore) {
        this.dataStore = dataStore;
    }

    public Datastore getDataStore() {
        return this.dataStore;
    }

    public Morphia getMorphia() {
        return this.morphia;
    }

    public void setMorphia(final Morphia morphia) {
        this.morphia = morphia;
    }

}
